import { reactive, watchEffect } from 'vue'

export const useStorage = <T>(name: string, value = {}): T => {
   const storedValue = localStorage.getItem(name)
   const initialValue = storedValue ? JSON.parse(storedValue) : value
   const data = reactive(initialValue)

   watchEffect(() => {
      localStorage.setItem(name, JSON.stringify(data))
   })

   return data
}
